package edu.qichen.onlineBlog.log.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.qichen.onlineBlog.framework.mybatis.core.query.LambdaQueryWrapperX;
import edu.qichen.onlineBlog.log.mapper.LoginLogMapper;
import edu.qichen.onlineBlog.log.pojo.LoginLog;
import edu.qichen.onlineBlog.log.service.LoginLogService;
import edu.qichen.onlineBlog.log.vo.PageLoginLogReq;
import edu.qichen.onlineBlog.log.vo.PageLoginLogRes;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2024/6/21 15:58
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertAll(List<LoginLog> loginLogs) {
        for (LoginLog loginLog : loginLogs) {
            baseMapper.insert(loginLog);
        }
    }

    @Override
    public PageLoginLogRes selectPage(PageLoginLogReq pageLoginLogReq) {
        Page<LoginLog> page = new Page<>(pageLoginLogReq.getPageNum(), pageLoginLogReq.getPageSize());

        LambdaQueryWrapperX<LoginLog> wrapperX = new LambdaQueryWrapperX<>();

        wrapperX.orderByDesc(LoginLog::getId);

        Page<LoginLog> logPage = baseMapper.selectPage(page, wrapperX);


        return PageLoginLogRes
                .builder()
                .loginLogs(logPage.getRecords())
                .pageNum(logPage.getCurrent())
                .pages(logPage.getPages())
                .pageSize(logPage.getSize())
                .total(logPage.getTotal())
                .build();
    }
}
